package org.com.system.util.sql;

import org.apache.commons.lang3.StringUtils;
import org.com.system.user.User;
import org.com.system.util.model.ActionValues;
import org.com.system.util.utils.StrUtils;

public class UpdateSQLGener {
	
	
	/**
     * 生成保存数据SQL,?符格式
     * 例：update table_name set column_a=?,column_b=? where id=?
     * @param model
     * @return
     */
    public static String updateSQL(Class<?> model){
    	String tableName=SQLUtils.getTableName(model);
    	StringBuffer stber=new StringBuffer("\tUPDATE  "+tableName+" \n\t\tSET "+StringUtils.join(SQLUtils.getColumnParams(model,SQLUtils.UPDATE_OPERATOR,false),",")+" \n\tWHERE ID=?");
    	return StrUtils.out(stber.toString());
    }

	/**
     * 生成保存数据SQL,?符格式
     * 例：update table_name set column_a=:column_a_param,column_b=:column_b_param where id=:id
     * @param model
     * @return
     */
    public static String updateParamsSQL(Class<?> model){
    	String tableName=SQLUtils.getTableName(model);
    	StringBuffer stber=new StringBuffer("\tUPDATE  "+tableName+" \n\t\tSET "+StringUtils.join(SQLUtils.getColumnParams(model,SQLUtils.UPDATE_OPERATOR,true),",")+" \n\tWHERE ID=:id");
    	return StrUtils.out(stber.toString());
    }
    
    /**
     * 生成保存数据SQL,参数格式
     * 例：update table_name set column_a=:column_a_value,column_b=:column_b_value where id=id_value
     * @param model
     * @return
     */
    public static String updateValuesSQL(ActionValues<String,Object> values,Class<?> model){
    	String tableName=SQLUtils.getTableName(model);
    	StringBuffer stber=new StringBuffer("\tUPDATE  "+tableName+" \n\t\tSET "+StringUtils.join(SQLUtils.getColumnValues(values,model,false)," , ")+" \n\tWHERE ID='"+values.getString("id")+"'");
    	return StrUtils.out(stber.toString());
    }
    
    public static void main(String[] args) {
		UpdateSQLGener.updateSQL(User.class);
		UpdateSQLGener.updateParamsSQL(User.class);
	}
	
}
